<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Column;
use addons\BuilderPlus\components\Search;
use addons\BuilderPlus\facade\Table;
use app\common\tool\Result;
use think\Response;

class Spec extends Base
{
    public function getOptions(): array
    {
        return [
            'pk' => 'id',
            'model' => \app\common\model\Spec::class,
            'append' => ['specText'],
            'just' => false,
            'fast' => [
                'add' => 'spec/edit',
                'edit' => 'spec/edit',
                'save' => '_save'
            ]
        ];
    }

    public function index(): string
    {
        return Table::init($this)
            ->addColumns([
                ['id', 'ID', fn(Column $col) => $col->width(80)],
                ['title', '规格模板名称'],
                ['spec', '规格值', fn(Column $col) => $col->templet('#specTpl')],
                ['create_time', '添加时间', fn(Column $col) => $col->width(150)],
                ['update_time', '更新时间', fn(Column $col) => $col->width(150)]
            ])
            ->addTopButtons(['add', 'deleteAll'])
            ->addRightButtons(['edit', 'delete'])
            ->addSearch([
                ['create_time', '添加时间', fn(Search $search) => $search->type('date')],
                ['title', '规格模板名称', fn(Search $search) => $search->condition('Like')]
            ])
            ->addTpl(['spec'])
            ->setConfig([
                'lineStyle' => 'min-height: 40px'
            ])
            ->fetch();
    }

    public function getSpec($id = '', $type = 1): Response
    {
        if ($type == 1) {
            $list = $this->model::where(['id' => $id])->append(['specText'])->find();
        } else {
            $list = $this->model::field('id,title')->order('id desc')->select();
        }

        return Result::success('success', $list);
    }
}